package scala.for_the.impatient

/**
  * Created by shichen on 2017/5/24.
  */

class Person5 {
  //重写非抽象方法必须使用override
  override def toString: String = getClass.getName + " [name=cs]"
  protected val name: String = "" //对类所属不可见？
}

class Employee extends Person5 {
  //调用超类方法用super
  override def toString: String = super.toString + "[salary=100]"
}

//scala类继承java类，主构造器必须调用java超类的某一个构造方法
class Square(x:Int, y: Int,width:Int) extends java.awt.Rectangle(x,y,width,width) {

}

abstract class Person6(val name: String) {
  def id: Int //抽象方法不需用abstract关键字，只省去方法体

  val numId: Int //带有抽象的getter方法的抽象字段
  var addr: String //带有抽象的getter/setter方法的抽象字段

}
class Employee2(name: String) extends Person6(name) {
  def id: Int = name.hashCode //重写超类抽象方法，override不需要使用

  val numId = 1 //具体子类必须提供具体的字段
  var addr = ""
}

object ch08_inheritance extends App{
  t_annymous_subclass()

  def t_annymous_subclass(): Unit = {
    val alien = new Person2("Fred") { //这创建的是一个结构类型的对象，看成是继承Person2的子类
      def greeting = "hello"
    }
    println(alien.greeting)
  }
}
